﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Quellenverwaltung.Meta
{
    class ReadMetaDataFromDOISite
    {
        public Dictionary<string, string> ReadMetaData(string url)
        {
            Dictionary<string, string> meta = new Dictionary<string, string>();
            meta.Add("journal_url", url);
            WebClientEncoding client = new WebClientEncoding();
            string html = client.DownloadPage(url);

            // Scan links on this page
            HtmlTag tag;
            HtmlParser parse = new HtmlParser(html);
            while (parse.ParseNext("meta", out tag))
            {
                // See if this anchor links to us
                string nameValue;
                string contentValue;

                if (tag.Attributes.TryGetValue("name", out nameValue)
                    && tag.Attributes.TryGetValue("content", out contentValue))
                {

                            if (!meta.ContainsKey(nameValue))
                            {
                                meta.Add(nameValue, contentValue);
                            }
                            else if (nameValue.Equals("citation_author"))
                            {
                                string author;
                                meta.TryGetValue("citation_author", out author);

                                if (!String.IsNullOrEmpty(author))
                                {
                                    author += ";";
                                    meta.Remove("citation_author");
                                }
                                author += contentValue;
                                meta.Add("citation_author", author);
                            }
                }
            }

            return meta;
        }

    }
}
